//
//  TZCommunicationViewController.m
//  wangYiCaiPiao
//
//  Created by mac on 16/1/4.
//  Copyright © 2016年 tz. All rights reserved.
//

#import "TZCommunicationViewController.h"
#import "TZPBLModel.h"
#import "TZPBLCell.h"
#import "TZLayout.h"



@interface TZCommunicationViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,SDPhotoBrowserDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) TZLayout *flowlayout;
@property (nonatomic, strong) NSArray *PBLArray;
@end

@implementation TZCommunicationViewController
static NSString * const reuseIdentifier = @"PBLCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationItem setTitle:@"开奖信息"];
    [self setupUI];
}

//更新UI
- (void)setupUI {
    //流式布局
    self.flowlayout = [[TZLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:self.flowlayout];
    //注册cell
    [self.collectionView registerClass:[TZPBLCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [self.view addSubview:self.collectionView];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.flowlayout.shops = self.PBLArray;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.PBLArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TZPBLCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[TZPBLCell alloc] init];
    }
    cell.model = self.PBLArray[indexPath.item];
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    SDPhotoBrowser *photoBrowser = [SDPhotoBrowser new];
    photoBrowser.delegate = self;
    photoBrowser.currentImageIndex = indexPath.item;
    photoBrowser.imageCount = self.PBLArray.count;
    photoBrowser.sourceImagesContainerView = self.collectionView;
    
    [photoBrowser show];
}
// 返回临时占位图片（即原来的小图）
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    // 不建议用此种方式获取小图，这里只是为了简单实现展示而已
    TZPBLCell *cell = (TZPBLCell *)[self collectionView:self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
    return cell.imageView.image;
    
}
// 返回高质量图片的url
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    NSString *urlStr = [[self.PBLArray[index] img] stringByReplacingOccurrencesOfString:@"" withString:@""];
    return [NSURL URLWithString:urlStr];
}

//懒加载
- (NSArray *)PBLArray {
    if (_PBLArray == nil) {
        NSArray *tempArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"puBuLiu.plist" ofType:nil]];
        NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:tempArray.count];
        for (NSDictionary *dict in tempArray) {
            TZPBLModel *model = [TZPBLModel PBLWithDict:dict];
            [mArray addObject:model];
        }
        _PBLArray = mArray;
    }
    return _PBLArray;
}
@end
